package com.pipayshop.controller;

import com.pipayshop.domain.dto.ApplyShopDTO;
import com.pipayshop.domain.dto.QrcodeDto;
import com.pipayshop.domain.dto.StoreInfoDTO;
import com.pipayshop.domain.dto.StoreInfoListByConditionDTO;
import com.pipayshop.domain.vo.*;
import com.pipayshop.entity.vo.BgImgVO;
import com.pipayshop.service.PhysicalStoreInfoService;
import com.pipayshop.entity.vo.ResponseVO;
import com.pipayshop.exception.BusinessException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * <p>
 * 实体店的信息 前端控制器
 * </p>
 *
 * @author nws
 * @since 2023-07-25
 */
@Api(value = "实体店的信息接口", tags = "实体店的信息接口")
@RestController
@RequestMapping("/pipayshopapi/store-info")
public class PhysicalStoreInfoController {
    @Resource
    private PhysicalStoreInfoService infoService;
    private static final Logger log = LoggerFactory.getLogger(PhysicalStoreTagsController.class);


    @PostMapping("getShopCodeByShopId/{shopId}")
    @ApiOperation("获取店铺的付款码链接")
    public ResponseVO<String> getShopCodeByShopId(@PathVariable String shopId) {
        try {
            String result = infoService.getShopCodeByShopId(shopId);
            return ResponseVO.getSuccessResponseVo(result);
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException("get_store_payment_code_link_failed");
        }
    }

    @PostMapping("setShopScore")
    @ApiOperation("根据评论评分设置网店评分")
    public ResponseVO setShopScore() {
        try {
            Boolean Residue = infoService.setShopScore();
            if (!Residue) {
                throw new BusinessException("comment_score_setting_failed_contact_admin");
            }
            return ResponseVO.getSuccessResponseVo(Residue);
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException("comment_score_setting_failed");
        }
    }

    @GetMapping("getShopInfoListByCondition")
    @ApiOperation("首页根据条件获取所有实体店列表")
    public ResponseVO<PageDataVO> getShopInfoListByCondition(StoreInfoListByConditionDTO shopInfoListByConditionDTO) {
        try {
            PageDataVO shopInfoListByCondition = infoService.getShopInfoListByCondition(shopInfoListByConditionDTO);
            return ResponseVO.getSuccessResponseVo(shopInfoListByCondition);
        } catch (BusinessException e) {
            throw new BusinessException(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException("get_all_store_list_failed");
        }
    }

    @GetMapping("InquireShop")
    @ApiOperation("根据条件获取所有自营店列表")
    public ResponseVO<PageDataVO> InquireShop(StoreInfoListByConditionDTO shopInfoListByConditionDTO) {
        try {
            PageDataVO shopInfoListByCondition = infoService.getInquireShop(shopInfoListByConditionDTO);
            return ResponseVO.getSuccessResponseVo(shopInfoListByCondition);
        }catch (BusinessException e) {
            throw new BusinessException(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException("get_all_self_operated_store_list_failed");
        }
    }

    @GetMapping("getShopInfoVO/{shopId}")
    @ApiOperation("根据实体店id查询实体店信息(我的)")
    public ResponseVO getShopInfoVO(@PathVariable String shopId) {
        try {
            StoreInfoVO1 shopInfoVO = infoService.getShopInfoVO(shopId);
            return ResponseVO.getSuccessResponseVo(shopInfoVO);
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException("query_store_info_failed");
        }
    }

    @GetMapping("getShopList")
    @ApiOperation("根据用户id查询实体店列表(我的)")
    public ResponseVO getShopList(UidPageVO uidPageVO) {
        try {
            PageDataVO shopList = infoService.getShopList(uidPageVO);
            return ResponseVO.getSuccessResponseVo(shopList);
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException("查询实体店列表失败");
        }
    }

    @GetMapping("getShopNumber/{uid}")
    @ApiOperation("根据用户id查询用户名下实体店数量")
    public ResponseVO getShopNumber(@PathVariable String uid) {
        try {
            Integer shopNumber = infoService.getShopNumber(uid);
            return ResponseVO.getSuccessResponseVo(shopNumber);
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException("查询失败");
        }
    }

    @GetMapping("getShopInfoById/{shopId}")
    @ApiOperation("根据id查询实体店详情")
    public ResponseVO getShopInfoById(@PathVariable String shopId) {
        try {
            StoreInfoVO shopByIdVO = infoService.getShopInfoById(shopId);
            return ResponseVO.getSuccessResponseVo(shopByIdVO);
        } catch (Exception e) {
            log.error(e.getMessage());
            throw new BusinessException("query_store_detail_failed");
        }
    }

    @PostMapping("undercarriageShopInfoById/{shopId}/{status}")
    @ApiOperation("根据实体id下上架实体店")
    public ResponseVO undercarriageShopInfoById(@PathVariable String shopId, @PathVariable Integer status) {
        try {
            infoService.deleteShopInfoById(shopId, status);
            return ResponseVO.getSuccessResponseVo("根据id下上架实体店成功");
        } catch (BusinessException e) {
            throw new BusinessException(e.getMessage());
        }catch (Exception e) {
            throw new BusinessException("change_failed");
        }
    }

    @PostMapping("updateShopInfoById")
    @ApiOperation("根据实体id修改实体店")
    public ResponseVO<String> updateShopInfoById(@RequestBody StoreInfoDTO shopInfoDTO) {
        try {
            infoService.updateShopInfoById(shopInfoDTO);
            return ResponseVO.getSuccessResponseVo("根据id修改实体店成功");
        }catch (BusinessException e) {
            throw new BusinessException(e.getMessage());
        } catch (Exception e) {
            throw new BusinessException("modify_store_failed");
        }
    }

    @PostMapping("applyShop")
    @ApiOperation("申请实体店")
    public ResponseVO<String> applyShop(ApplyShopDTO applyShopDTO) {
        try {
            infoService.applyShop(applyShopDTO);
            return ResponseVO.getSuccessResponseVo("申请实体店成功");
        }catch (BusinessException e) {
            throw new BusinessException(e.getMessage());
        } catch (Exception e) {
            throw new BusinessException("apply_store_failed");
        }
    }


    /**
     * 根据店铺id查询该店铺是否为vip店铺
     */
    @GetMapping("isVipShop/{shopId}")
    @ApiOperation("根据店铺id查询该店铺是否为vip店铺")
    public ResponseVO isVipShop(@PathVariable("shopId") String shopId) {
        try {
            boolean flag = infoService.isVipShop(shopId);
            return ResponseVO.getSuccessResponseVo(flag);
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException("查询失败");
        }
    }

    /**
     * 根据用户id查找名下的所有实体店铺的shopId列表
     */
    @GetMapping("getShopIdListByUid/{uid}")
    @ApiOperation("根据用户id查找名下的所有实体店铺的shopId列表")
    public ResponseVO getShopIdListByUid(@PathVariable("uid") String uid) {
        try {
            List<String> list = infoService.getShopIdListByUid(uid);
            return ResponseVO.getSuccessResponseVo(list);
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException("query_store_list_failed");
        }
    }

    /**
     * 将多家实体店一起升级为vip店铺
     */
    @PostMapping("upVipByShopIdList")
    @ApiOperation("将多家实体店一起升级为vip店铺")
    public ResponseVO<String> upVipByShopIdList(String shopIds) {
        try {
            Boolean update = infoService.upVipByShopIdList(shopIds);
            if (!update) {
                throw new BusinessException("upgrade_multiple_stores_to_vip_failed");
            }
            return ResponseVO.getSuccessResponseVo("将多家实体店一起升级为vip店铺");
        } catch (Exception e) {
            log.error(e.getMessage());
            throw new BusinessException("upgrade_vip_store_failed");
        }
    }

    @GetMapping("updateShopCommodity/{shopId}")
    @ApiOperation("查询指定实体店还可以上传的商品数量")
    public ResponseVO updateShopCommodity(@PathVariable String shopId) {
        try {
            Integer integer = infoService.updateShopCommodity(shopId);
            return ResponseVO.getSuccessResponseVo(integer);
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException("query_uploadable_commodity_count_failed");
        }
    }

    @PostMapping("shopTopImageUp")
    @ApiOperation("实体店展示图上传")
    public ResponseVO<String> shopTopImageUp(MultipartFile multipartFile) {
        try {
            String imageId = infoService.shopTopImageUp(multipartFile);
            if ("image_not_approved".equals(imageId)) {
                return ResponseVO.getFalseResponseMsg("image_not_approved");
            }
            return ResponseVO.getSuccessResponseVo(imageId);
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException("display_image_upload_failed");
        }
    }

    @PostMapping("shopImageUp")
    @ApiOperation("实体店轮播图上传")
    public ResponseVO<String> shopImageUp(MultipartFile multipartFile) {
        try {
            String imageId = infoService.shopImageUp(multipartFile);
            if ("image_not_approved".equals(imageId)) {
                return ResponseVO.getFalseResponseMsg("image_not_approved");
            }
            return ResponseVO.getSuccessResponseVo(imageId);
        }catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException("carousel_image_upload_failed");
        }
    }

    /**
     * 校验商家的付款ID
     */
    @PostMapping("checkId")
    @ApiOperation("校验商家的付款ID")
    public ResponseVO<CheckVO> checkId(@RequestBody QrcodeDto qrcode) {
        try {
            CheckVO checkId = infoService.checkId(qrcode.getQrcode());
            return ResponseVO.getSuccessResponseVo(checkId);
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException("check_merchant_payment_failed");
        }
    }

    /**
     * 获取二级行政区域列表
     */
    @GetMapping("getSecondDistrictList/{countryCode}")
    @ApiOperation("获取二级行政区域列表")
    public ResponseVO<List<CountryMinVO>> getSecondDistrictList(@PathVariable String countryCode) {
        try {
            List<CountryMinVO> list = infoService.getSecondDistrictList(countryCode);
            if (list == null) {
                throw new BusinessException("get_second_level_region_list_failed");
            }
            return ResponseVO.getSuccessResponseVo(list);
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException("get_second_level_region_list_failed");
        }
    }

    /**
     * 获取三级行政区域列表
     */
    @GetMapping("getThirdDistrictList/{countrySecondId}")
    @ApiOperation("获取三级行政区域列表")
    public ResponseVO<List<CountryMinVO>> getThirdDistrictList(@PathVariable String countrySecondId) {
        try {
            List<CountryMinVO> list = infoService.getThirdDistrictList(countrySecondId);
            if (list == null) {
                throw new BusinessException("get_third_level_region_list_failed");
            }
            return ResponseVO.getSuccessResponseVo(list);
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException("get_third_level_region_list_failed");
        }
    }

    /**
     * 获取四级行政区域列表（针对中国出发）
     */
    @GetMapping("getFourthDistrictList/{countryThirdId}")
    @ApiOperation("获取四级行政区域列表（针对中国出发）")
    public ResponseVO<List<CountryMinVO>> getFourthDistrictList(@PathVariable String countryThirdId) {
        try {
            List<CountryMinVO> list = infoService.getFourthDistrictList(countryThirdId);
            if (list == null) {
                throw new BusinessException("get_fourth_level_region_list_failed");
            }
            return ResponseVO.getSuccessResponseVo(list);
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException("get_fourth_level_region_list_failed");
        }
    }


    /*
    @PostMapping("piIdImageUp")
    @ApiOperation("pi_Id_Image上传")
    public ResponseVO<String> piIdImageUp(MultipartFile multipartFile){
        try {
            String piIdImageUp = FileUploadUtil.uploadFile(multipartFile, FileUploadUtil.PI_ID_IMAGE);
            return ResponseVO.getSuccessResponseVo(piIdImageUp);
        }catch (Exception e){
            e.printStackTrace();
            throw new BusinessException("pi_Id_Image上传失败，请联系后台人员");
        }
    }*/

    @PostMapping("/deleteShopInfoById/{shopId}")
    @ApiOperation("删除实体店")
    public ResponseVO deleteShopInfoById(@PathVariable String shopId) {
        try {
            int i = infoService.deleteShopById(shopId);
            if (i == 1) {
                return ResponseVO.getSuccessResponseVo("delete_success");
            }
            else{
                throw new BusinessException("delete_store_failed");
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException("delete_store_failed");
        }
    }
}
